package com.songlanyun.modules.studying.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.studying.entity.Studying;
import com.songlanyun.modules.studying.service.StudyingService;
import com.songlanyun.modules.studyingType.entity.StudyingType;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/studying")
public class StudyingController extends AbstractController {


    @Autowired
    private StudyingService studyingService;


    @GetMapping("/list")
//    @RequiresPermissions("base:studying:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = studyingService.queryPage(params);
        return R.ok().put("page", page);
    }


    @GetMapping("/{id}")
//    @RequiresPermissions("base:studying:info")
    public R info(@PathVariable("id") Long id) {
        Studying studying = studyingService.loadById(id, true);
        return R.ok().put("data", studying);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
//    @RequiresPermissions("base:studying:add")
    public R save(@RequestBody Studying studying) {
        ValidatorUtils.validateEntity(studying);
        studying.setCreateBy(getUserId());
        studying.setUpdateBy(getUserId());
        Studying resstudying = studyingService.add(studying);
        return R.ok().put("data", resstudying);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
//    @RequiresPermissions("base:studying:update")
    public R update(@RequestBody Studying studying) {
        if (ObjectUtil.isNull(studying.getId())) {
            throw new RRException("ID不能为空");
        }
        ValidatorUtils.validateEntity(studying);
        studying.setUpdateBy(getUserId());
        Studying resstudying = studyingService.update(studying);
        return R.ok().put("data", resstudying);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("base:studying:delete")
    public R delete(@PathVariable("id") Integer id) {
        studyingService.removeById(id);
        return R.ok();
    }

    /**
     * 下架取消推荐    type=hasStatus下架  type=hasHeader推荐
     */
    @PostMapping("/headerORstatus")
//    @RequiresPermissions("base:studying:delete")
    public R headerORstatus(@RequestBody Map<String, Object> params) {
        if (ObjectUtil.isNull(params.get("id"))) {
            throw new RRException("请选择要操作的数据");
        }
        Integer id = Integer.valueOf(params.get("id").toString());
        if (ObjectUtil.isNull(params.get("type"))) {
            throw new RRException("执行操作不能为空");
        }
        String type = params.get("type").toString();
        studyingService.headerStatus(id, type);
        return R.ok();
    }
}
